var data_baseline;
function OpenTab(widget,id_widget){
  Ext.getCmp('maincontent').setTitle("");
  className = Ext.ClassManager.getNameByAlias(widget);
  ViewClass = Ext.ClassManager.get(className);
  cmp = new ViewClass();
  if(Ext.getCmp('maincontent').getComponent(id_widget)) 
   {    if (id_widget=="addnewsitemaster"){
	   
	   var tab = Ext.getCmp(id_widget);
       Ext.getCmp("maincontent").remove(tab);
       Ext.getCmp('maincontent').add(cmp).show();
	   Ext.getCmp('maincontent').setActiveTab(cmp);
	   Ext.resumeLayouts(true);
   }else{
	   
	   Ext.getCmp('maincontent').setActiveTab(cmp);
	
   }
   } else 
   
   {
	    Ext.getCmp('maincontent').add(cmp).show();
	    Ext.getCmp('maincontent').setActiveTab(cmp);
	    Ext.resumeLayouts(true);
	}
}


function PostData(data,url,operation){
	var return_data = "";
 $.ajax({
		    type: 'POST',
		    data: data,
		    url: url,
		    dataType: 'json',
		    async: false,
		    success: function(result){
		    	if (result.responseText=="success"){
		    		Ext.Msg.alert('Result', ''+operation+' Success!!');
		    		return_data = "success";
		    	}
		    },
		    error: function(jqXHR, textStatus, errorThrown){
		    	//console.log (jqXHR);
		    	if (jqXHR.status==500 || jqXHR.status== 404){
		    		Ext.Msg.alert('Result', ''+operation+' Failed, '+jqXHR.statusText);	
		    		return_data = "failed";
		    	}else{
		    		if (jqXHR.responseText=="success"){
			    		Ext.Msg.alert('Result', ''+operation+' Success!!');
			    		return_data = "success";
			    	}else{
			    		Ext.Msg.alert('Result', ''+operation+' Failed, '+jqXHR.responseText);	
			    		return_data = "failed";
			    	}
		       
		    	}
		    	
	        
		    }
	  });
}

function afterSubmitInformation(form, action){
	 Ext.Msg.alert('Submited', action.response.responseText);
}

function tabchange(id){
	 if (id=="relocationsite"){
		 Ext.getStore('Administration.AssetChangeReason').removeAll();
         Ext.getStore('Administration.AssetChangeReason').getProxy().url = 'Services/DataMaster/AssetChangeReason/List?CHANGE_CATEGORY=RELOCATION_SITE';
         Ext.getStore('Administration.AssetChangeReason').load();
	 }else if (id=="relocationne"){
		 Ext.getStore('Administration.AssetChangeReason').removeAll();
         Ext.getStore('Administration.AssetChangeReason').getProxy().url = 'Services/DataMaster/AssetChangeReason/List?CHANGE_CATEGORY=RELOCATION_NE';
         Ext.getStore('Administration.AssetChangeReason').load();
		   
	 }else if (id == "hentioperasisite"){
		 Ext.getStore('Administration.AssetChangeReason').removeAll();
         Ext.getStore('Administration.AssetChangeReason').getProxy().url = './Services/DataMaster/AssetChangeReason/List?CHANGE_CATEGORY=HENTI_OPERASI_SITE';
         Ext.getStore('Administration.AssetChangeReason').load();
		 
	 }else if (id == "hentioperasine"){
		  Ext.getStore('Administration.AssetChangeReason').removeAll();
	      Ext.getStore('Administration.AssetChangeReason').getProxy().url = './Services/DataMaster/AssetChangeReason/List?CHANGE_CATEGORY=HENTI_OPERASI_NE';
	      Ext.getStore('Administration.AssetChangeReason').load();	
	      
	 }else if (id == "dismantlesiteform"){
		  Ext.getStore('Administration.AssetChangeReason').removeAll();
	      Ext.getStore('Administration.AssetChangeReason').getProxy().url = './Services/DataMaster/AssetChangeReason/List?CHANGE_CATEGORY=DISMANTLE_SITE';
	      Ext.getStore('Administration.AssetChangeReason').load();
	      
	 }else if (id == "dismantleNE"){
		  Ext.getStore('Administration.AssetChangeReason').removeAll();
	      Ext.getStore('Administration.AssetChangeReason').getProxy().url = './Services/DataMaster/AssetChangeReason/List?CHANGE_CATEGORY=DISMANTLE_NE';
	      Ext.getStore('Administration.AssetChangeReason').load();	 
	 }
	
}



function MakeMenuItems (i,text,widget,id){
	Ext.getCmp('jajan'+i).add(
             Ext.create('Ext.menu.Item', {
          	   	floating: false,
				    //text    :data_baseline.children[i].children[j].text,
				    text    :text,
				    width	: 250,
				    //icon    :'images/icon-dashboard.png',
				   listeners: {
	            	   click: function(){
	            		   if (text=="Logout"){
	            			   location.href='./Services/Security/User/Logout';  
	            		   }else{
	            			   OpenTab(widget,id);   
	            		   }
	            		  
							}
	            
		    			},
             }));
}

function MakeMenu(){
	 Ext.Ajax.request({
	        method: "GET",
	        url: './Services/Security/User/Menu',
	        async: false,
	        success: function(c) {
	            data_baseline = JSON.parse(c.responseText);
	            for (var i =0; i<data_baseline.children.length; i++){
	                 
	            	Ext.getCmp("listmenu").add(
	                       Ext.create('Ext.menu.Menu', {
	                    	   	floating: false,
							    title   : data_baseline.children[i].text,
							    id      : 'jajan'+i,
							    width	: 250,
							    icon    :'images/icon/'+data_baseline.children[i].image
	                       }));
	            	
	            	if(data_baseline.children[i].children){
	            		for (var j=0; j<data_baseline.children[i].children.length; j++){
		            		 var text = data_baseline.children[i].children[j].text;
		            		 var widget = data_baseline.children[i].children[j].widget;
		            		 var id = data_baseline.children[i].children[j].id;
		            		 MakeMenuItems (i,text,widget,id);
		            	 }
	            	}
	            	
	            }
	           
	            
	        }
	    });
	
}

function hasPrivilege(priv){
	var privillage = "";
	 $.ajax({
			    type: 'GET',
			    data: data,
			    url: './Services/Security/User/hasPrivilege/'+priv,
			    dataType: 'json',
			    async: false,
			    success: function(result){
			    	privillage = result;
			    },
			    error: function(jqXHR, textStatus, errorThrown){
			    	//console.log (jqXHR);
			    	if (jqXHR.status==500 || jqXHR.status== 404){
			    		Ext.Msg.alert('Result', ''+operation+' Failed, '+jqXHR.statusText);	
			    	}else{
			    		if (jqXHR.responseText=="success"){
				    		Ext.Msg.alert('Result', ''+operation+' Success!!');
				    	}else{
				    		Ext.Msg.alert('Result', ''+operation+' Failed, '+jqXHR.responseText);	
				    	}
			       
			    	}
			    	
		        
			    }
		  });
	 
	 return privillage;
	}

function DefinePrivillage(Privillage,component){
    	 if (hasPrivilege(Privillage)=="0"){
    		 component.setVisible(false);
    	 }else{ 
    		 component.setVisible(true); 
    	 }
}


